package com.train.common.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

@EnableAsync
@Configuration
public class ThreadPoolConfig {

    private static final int CORE_THREAD = Runtime.getRuntime().availableProcessors() + 1;

    private static final int MAX_THREAD = CORE_THREAD * 2;

    @Bean
    public static ThreadPoolExecutor getThreadPool() {
        return new ThreadPoolExecutor(
                CORE_THREAD,
                MAX_THREAD,
                60,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(1000),
                new ThreadPoolExecutor.CallerRunsPolicy());
    }
}
